<template>
	<view class="publish-page">

		<view v-if="publishState==2" class="item item-padding">
			<view class="title">选择广告形式</view>
			<view class="picker">
				<picker style="width: 100%;" :range="formData" @change="formChange" :value="formIndex">
					<view class="pciker-content">
						<image class="arrow" src="../../static/img/turn.png"></image>
						<view class="pciker-text">{{formData[formIndex]}}</view>
					</view>
				</picker>
			</view>
		</view>
		<view v-if="publishState==2&&isSell&&userType==20" class="item item-padding">
			<view class="title">选择交易形式</view>
			<view class="picker">
				<picker style="width: 100%;" :range="tradeData" @change="tradeChange" :value="tradeIndex">
					<view class="pciker-content">
						<image class="arrow" src="../../static/img/turn.png"></image>
						<view class="pciker-text">{{tradeData[tradeIndex]}}</view>
					</view>
				</picker>
			</view>
		</view>

		<view v-if="publishState==2" class="item item-padding">
			<view class="title">投放到</view>
			<view class="picker">
				<picker style="width: 100%;" :range="channelData" @change="channelChange" :value="channelIndex">
					<view class="pciker-content">
						<image class="arrow" src="../../static/img/turn.png"></image>
						<view class="pciker-text">{{channelData[channelIndex]}}</view>
					</view>
				</picker>
			</view>
		</view>

		<view v-if="publishState==2&&channelIndex==1" class="item item-padding">
			<view class="title">选择类目</view>
			<view class="picker">
				<picker style="width: 100%;" :range="categoryData" @change="categoryChange" :value="categoryIndex">
					<view class="pciker-content">
						<image class="arrow" src="../../static/img/turn.png"></image>
						<view class="pciker-text">{{categoryData[categoryIndex]}}</view>
					</view>
				</picker>
			</view>
		</view>
		
		<view v-if="publishState==2&&userType==0" class="item item-padding">
			<view class="title">只允许新用户收看</view>
			<!-- <view class="picker"> -->
			<view :class="isAli?'':'picker'">
				<picker style="width: 100%;" :range="newSeeData" @change="newSeeChange" :value="newSeeIndex">
					<view class="pciker-content">
						<image class="arrow" src="/static/img/turn.png"></image>
						<view class="pciker-text">{{newSeeData[newSeeIndex]}}</view>
					</view>
				</picker>
			</view>
		</view>

		<view v-if="publishState==1" class="ad-info">编辑信息</view>
		<view v-if="publishState==2" class="ad-info">广告信息</view>
		<view class="item">
			<view v-if="publishState==1" class="title">店铺主题</view>
			<view v-if="publishState==2" class="title">广告标题</view>
			<input v-if="publishState==1" type="text" class="item-input" value="" v-model="title"
				placeholder="请输入店铺主题" />
			<input v-if="publishState==2" type="text" class="item-input" value="" v-model="title"
				placeholder="请输入广告标题" />
		</view>
		<view class="item">
			<view v-if="publishState==1" class="title">商品介绍</view>
			<view v-if="publishState==2" class="title">广告内容</view>
			<input v-if="publishState==1" type="text" class="item-input" value="" v-model="content"
				placeholder="请输入商品介绍" />
			<input v-if="publishState==2" type="text" class="item-input" value="" v-model="content"
				placeholder="请输入广告内容" />
		</view>

		<view class="item item-padding">
			<view v-if="publishState==1" class="title">介绍类型</view>
			<view v-if="publishState==2" class="title">广告类型</view>
			<view class="picker">
				<picker style="width: 100%;" :range="typeData" @change="typeChange" :value="formIndex">
					<view class="pciker-content">
						<image class="arrow" src="../../static/img/turn.png"></image>
						<view class="pciker-text">{{typeData[typeIndex]}}</view>
					</view>
				</picker>
			</view>
		</view>
		
		<view class="item item-padding" v-if="showCoupon && channelIndex == 1">
			<view class="title">是否使用优惠券</view>
			<!-- <view class="picker"> -->
			<view :class="isAli?'':'picker'">
				<picker style="width: 100%;" :range="couponArr" @change="couponChange" :value="couponIndex">
					<view class="pciker-content">
						<image class="arrow" src="../../static/img/turn.png"></image>
						<view class="pciker-text">{{couponArr[couponIndex]}} {{couponIndex=='0'?`(剩余条数:${couponData.videoNum})`:''}}</view>
					</view>
				</picker>
			</view>
		</view>

		<view v-if="publishState==2" class="item">
			<view class="title">单条广告价格 (元)</view>
			<input type="text" class="item-input" value="" v-model="price" :disabled="disablePrice" placeholder="请输入单条广告定价" />
		</view>

		<view style="flex-direction: column;padding-top: 20upx;" class="item" @click="chooseImage">
			<view v-if="publishState==1&&typeIndex==0" class="title">添加图片</view>
			<view v-if="publishState==2&&typeIndex==0" class="title">广告图片</view>
			<view v-if="publishState==1&&typeIndex == 1" class="title">添加视频封面图片</view>
			<view v-if="publishState==2&&typeIndex == 1" class="title">广告视频封面图片</view>
			<view class="add-wrap">
				<image v-if="!imagePath" src="../../static/img/add_img.png" mode="" class="add-img"></image>
				<image v-else :src="imagePath" mode="" class=""></image>
			</view>
		</view>

		<view v-if="typeIndex == 1" style="flex-direction: column;" class="item" @click="chooseVideo">
			<view class="video-title">
				<view v-if="publishState==1" class="title">添加视频</view>
				<view v-if="publishState==2" class="title">广告视频</view>
				<image src="../../static/img/shangc.png" mode="" class="icon-cloud"></image>
			</view>
			<view v-if="!videoPath" class="video-hint">
				注：视频不能超过60M，或不能大于40s，否则文件过大，上传失败
			</view>
			<view v-else class="video-wrap">
				<video :src="videoPath" controls></video>
			</view>
		</view>

		<view v-if="publishState==1" class="ad-info">商铺地址</view>
		<view v-if="publishState==2" class="ad-info">广告投放地址</view>
		<view class="item">
			<view v-if="publishState==1" class="title">商铺所属地区</view>
			<view v-if="publishState==2" class="title">广告投放所属地区</view>
			<view class="picker">
				<picker style="width: 100%;" mode="multiSelector" :range="dmultiArray"
					@columnchange="bindMultiPickerColumnChange" :value="dmultiIndex">
					<view class="pciker-content">
						<image class="arrow" src="../../static/img/turn.png"></image>
						<view class="pciker-text">
							{{dmultiArray[0][dmultiIndex[0]]}}，{{dmultiArray[1][dmultiIndex[1]]}}，{{dmultiArray[2][dmultiIndex[2]]}}，{{dmultiArray[3][dmultiIndex[3]]}}
						</view>
					</view>
				</picker>
			</view>
		</view>

		<view v-if="publishState==2" class="item item-padding" @click="chooseLocation">
			<view class="title">广告投放详细位置</view>
			<view class="picker">
				<view class="pciker-content">
					<image class="arrow" src="../../static/img/turn.png"></image>
					<view v-if="address" class="pciker-text">{{address}}</view>
					<view v-else class="pciker-text pciker-text-gray">请选择广告详细投放地址</view>
				</view>
			</view>
		</view>

		<view class="item">
			<view v-if="publishState==1" class="title">商铺详细地址</view>
			<view v-if="publishState==2" class="title">广告投放详细地址</view>
			<input type="text" class="item-input" value="" v-model="addressName" placeholder="xx大厦x层xxxx室" />
		</view>

		<view v-if="publishState==2" class="item">
			<view class="title">投放范围(km)</view>
			<input type="number" class="item-input" value="" v-model="distance" placeholder="请输入投放范围" />
		</view>
		
		<view v-if="prod.prodName" class="prod">带货商品</view>
		<view v-if="prod.prodName" class="item item-prod">
			<image :src="prod.pic" mode="aspectFit" class="prod-img"></image>
			<view class="prod-r">
				<view class="prod-text">{{prod.prodName}}</view>
				<view class="prod-price">￥{{prod.price}}</view>
			</view>
		</view>

		<view v-if="publishState==1" class="next" @click="next()">
			<text>提交</text>
		</view>
		<view v-if="publishState==2 && isUsed == 0" class="next" @click="sales()" style="margin-bottom: 20upx;">
			<text>带货</text>
		</view>
		<view v-if="publishState==2" class="next" @click="next()">
			<text>下一步</text>
		</view>

		<uni-popup ref="popup" background-color="#fff">
			<view class="btn-wrap">
				<view class="btn-title">抖音授权</view>
				<view class="btn-hint">获取您的抖音公开信息（昵称、头像）, 用于在您的个人信息中展示</view>
				<button type="primary" @click="getUserInfo">点击授权</button>
			</view>
		</uni-popup>



	</view>
</template>

<script>
	import uniPopup from "@/components/uni-popup/uni-popup.vue";
	export default {
		components: {
			uniPopup
		},
		data() {
			return {
				formData: ['展示', '采集', '销售', '任务', '外卖'],
				formIndex: 0,
				tradeData: ['到店核销', '快递邮寄'], //交易类型
				tradeIndex: 0,
				pickupMethod: 1, //交易类型,传给后台的字段
				isSell: 0,
				isCollect: 0,
				channelData: ['支付后', '视频号'],
				channelIndex: 1,
				channelId: 2,
				categoryData: [], //视频类目
				categoryIndex: 0,
				categoryId: 1,
				newSeeIndex: 1,
				newSeeData: [ "是","否" ],
				newSeeId: 2,

				typeData: ['图片', '视频'],
				typeIndex: 0,
				imagePath: '', //图片本地路径
				imageId: '', //图片上传成功返回的id
				videoPath: '', //视频文件本地路径
				videoId: '', //视频上传成功返回的id
				latitude: '',
				longitude: '',
				address: '', //广告详细投放地址
				price: '', //单条广告价格
				userId: '',
				adId: '', //广告id
				adressId: '', //广告位置的id

				dnui1: [],
				dnui2: [],
				dnui3: [],
				dnui4: [],
				dmultiArray: [
					[],
					[],
					[],
					[]
				],
				dmultiIndex: [0, 0, 0, 0],
				provinceId: '',
				cityId: '',
				areaId: '',
				streetId: '',

				nickName: '',
				avatarUrl: '',
				isIOS: true,
				publishState: 2,
				mainid: '',
				userType: 99, //99:普通用户，20：商家

				prod:{
				}, //带货商品
				isUsed:0, //是否使用卡券
				disablePrice:false,
				couponData:{},
				showCoupon:false,
				couponArr:['是','否'],
				couponIndex:1,
				title: "",
				content: "",
				addressName: "",
				distance: ""
			}
		},
		onLoad() {
			// uni.getStorage({
			// 	key: 'user',
			// 	success: res => {
			// 		this.mainid = res.data.id;
			// 		console.log("openid: ", this.mainid);
			// 		this.getApiUserInfo()
			// 	}
			// })
			this.getProvider()
			this.getProvinces()
			this.getCategory()
		},
		onShow() {
			console.log("onshow..");
			var that = this;
			uni.getStorage({
				key: 'user',
				success: res => {
					console.log("ressssssssss",res);
					that.mainid = res.data.id;
					console.log("openid: ", that.mainid);
					that.getApiUserInfo()
					that.checkSystem()
					that.getPublishState()
					//查询优惠券
					that.getCouponList()
				}
			})
		},
		// onReady() {
		// 	this.getProvider()
		// },
		methods: {
			getCouponList() {
				uni.request({
					header: {
						'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
					},
					url: getApp().globalData.url + 'ad/getCouponList/' + this.mainid, //仅为示例，并非真实接口地址。
					method: 'POST',
					data:{id: this.mainid},
					success: (res) => {
						console.log('卡券：', res);
						this.couponData = res.data.data[0]
						if(this.couponData&&this.couponData.videoNum>0){
							this.showCoupon = true
						}
					},
					fail: (error) => {
						console.log(error)
					}
				});
			},
			checkSystem() {
				uni.getStorage({
					key: 'app_plat',
					success: res => {
						console.log('平台：', res);
						if ('ios' == res.data) {
							this.isIOS = true
							// this.getPublishState()
						} else {
							this.isIOS = false
						}
						this.getPublishState()
					}
				})
			},
			getPublishState() {
				uni.getSystemInfo({
					success: (res) => {
						let brand = res.brand.toLowerCase()
						let id = 1453982355387654100
						switch (brand) {
							case 'xiaomi':
								id = 1456871784196436000;
								break;
							case 'huawei':
								id = 1456869585609388000;
								break;
							case 'vivo':
								id = 1456872496989040600;
								break;
							case 'oppo':
								id = 1456872951546736600;
								break;
							default:
								break;
						}
						uni.request({
							header: {
								"Content-Type": "application/x-www-form-urlencoded", //自定义请求头信息
							},
							url: getApp().globalData.url + 'admin/user/getAppletId/' + id,
							data: {
								id: id
							},
							method: 'POST',
							success: (res) => {
								console.log("publish app发布状态: ", res);
								this.publishState = res.data.data.state
								if (this.publishState == 2) {
									uni.setNavigationBarTitle({
										title: '发布广告'
									})
								} else {
									uni.setNavigationBarTitle({
										title: '商家信息'
									})
								}
							},
							fail: (err) => {
								console.log("publish app发布状态 err: ", err);
							}
						})
					}
				})
			},

			checkTtAuth() {
				let user = uni.getStorageSync('tt_user')
				console.log("tt_user: ", user);
				if (user) {
					// this.ttUser = user
				} else {
					setTimeout(res => {
						this.showAuth()
					}, 1000);
				}

			},

			showAuth() {
				this.$refs.popup.open('center')
			},
			closeAuth() {
				this.$refs.popup.close()
			},
			getProvider() {
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						this.provider = res.provider[0]
					}

				})
			},
			//获取头条用户信息
			getUserInfo() {
				console.log("this.provider: ", this.provider);
				this.closeAuth()
				uni.login({
					success: (res) => {
						uni.getUserInfo({
							provider: this.provider,
							success: (res) => {
								this.saveUserInfo(res.userInfo)
							},
							fail: (error) => {
								console.log('getUserInfo fail', error);
								let content = error.errMsg;
								if (~content.indexOf('uni.login')) {
									content = '请在登录页面完成登录操作';
								}
								uni.showModal({
									title: '获取用户信息失败',
									content: '错误原因' + content,
									showCancel: false
								});
							}
						});
					}
				})

			},
			//获取后台用户信息
			getApiUserInfo() {
				var that = this;
				uni.request({
					header: {
						'Content-Type': 'application/json', //自定义请求头信息
					},
					url: getApp().globalData.url + 'user/myInfo/' + that.mainid, //仅为示例，并非真实接口地址。
					method: 'POST',
					success: (res) => {
						console.log('个人信息 publish：', res);
						if(res.data.data.wechatAppletId == "" || !res.data.data.wechatAppletId || res.data.data.wechatAppletId == null){
							uni.showModal({
								title: '友情提醒',
								content: '您还未绑定微信小程序，请前往招财阿咪小程序进行绑定',
								showCancel: false,
								success: function (res) {
									if (res.confirm) {
										console.log('用户点击确定');
										uni.switchTab({
											url: '/pages/index/index'
										});
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						}
						that.userType = res.data.data.type
						if (that.userType == 20) {
							that.tradeData = ['到店核销', '快递邮寄']
						} else if (that.userType == 99) {
							that.pickupMethod = 2
						}
					},
					fail: (error) => {
						// this.checks = true
						console.log(error)
					}
				});
			},
			//保存头条用户信息
			saveUserInfo(user) {
				uni.setStorage({
					key: 'tt_user',
					data: user,
					success: (res) => {
						this.ttUser = user
						this.register(user.avatarUrl, user.nickName)
					}
				})
			},
			//注册
			register(avatarUrl, nickName) {
				// #ifdef MP-ALIPAY
				let plat = 4
				// #endif
				// #ifdef MP-WEIXIN
				let plat = 5
				// #endif
				// #ifdef MP-TOUTIAO
				let plat = 6
				// #endif
				uni.login({
					success: (res) => {
						console.log("uni login res: ", res);
						let data = {
							"code": res.code,
							"plat": plat,
							"nickName": nickName,
							"headUrl": avatarUrl
						}
						console.log("注册参数: ", data);
						uni.request({
							header: {
								"Content-Type": "application/json", //自定义请求头信息
							},
							url: getApp().globalData.url + 'user/register',
							data: data,
							method: 'POST',
							success: (res) => {
								console.log("注册成功: ", res);

							}
						})

					}
				})

			},
			//选择广告形式
			formChange(e) {
				console.log("e: ", e);
				this.formIndex = e.detail.value
				if (this.formIndex == 0) {
					this.isSell = 0
					this.isCollect = 0
				} else if (this.formIndex == 1) {
					this.isSell = 0
					this.isCollect = 1
				} else if (this.formIndex == 2) {
					this.isSell = 1
					this.isCollect = 0
				}
			},
			//选择交易方式：到店核销，发货
			tradeChange(e) {
				this.tradeIndex = e.detail.value
				this.pickupMethod = parseInt(this.tradeIndex) + 1
			},
			//选择投放渠道
			channelChange(e) {
				console.log("channelChange: ", e);
				this.channelIndex = e.detail.value
				this.channelId = parseInt(this.channelIndex) + 1
			},
			newSeeChange(e){
				console.log("newSeeChange: ", e);
				this.newSeeIndex = e.detail.value
				this.newSeeId = parseInt(this.newSeeIndex) + 1
				console.log("this.newSeeId",this.newSeeId);
			},
			//选择视频类目
			categoryChange(e) {
				console.log("categoryChange: ", e);
				this.categoryIndex = e.detail.value
				this.categoryId = parseInt(this.categoryIndex) + 1
			},
			//选择广告类型
			typeChange(e) {
				console.log("e: ", e);
				this.typeIndex = e.detail.value
			},
			//选择优惠券
			couponChange(e) {
				console.log("e: ", e);
				this.couponIndex = e.detail.value
				if(this.couponIndex=='0'){
					this.isUsed = 1
					this.price = 0.02
					this.disablePrice = true
				}else{
					this.isUsed =0
				}
			},
			bindMultiPickerColumnChange(e) {
				console.log("bindMultiPickerColumnChange: ", e);
				this.dmultiIndex[e.detail.column] = e.detail.value;
				if (e.detail.column == 0) {
					this.provinceId = this.dnui1[e.detail.value].id;
					this.cityId = '';
					this.areaId = '';
					this.streetId = '';
					this.dmultiIndex = [e.detail.value, 0, 0, 0];
					this.getCities();
				} else if (e.detail.column == 1) {
					this.cityId = this.dnui2[e.detail.value].id;
					this.areaId = '';
					this.streetId = '';
					this.dmultiIndex[2] = 0;
					this.dmultiIndex[3] = 0;
					this.getAreas();
				} else if (e.detail.column == 2) {
					this.areaId = this.dnui3[e.detail.value].id;
					this.streetId = '';
					this.dmultiIndex[3] = 0;
					this.getStreets();
				} else if (e.detail.column == 3) {
					this.dmultiIndex[3] = e.detail.value;
					this.streetId = this.dnui4[e.detail.value].id;
					// this.darray = [{
					// 	'name': '',
					// 	'id': ''
					// }];
					// this.xiaoqu();
				}
				this.$forceUpdate()
			},
			chooseImage() {
				console.log('choose...');
				uni.chooseImage({
					count: 1,
					sourceType: ['album'],
					success: (res) => {
						console.log("图片选择结果: ", res);
						this.imagePath = res.tempFilePaths[0]
						this.uploadImage()
					}
				})

			},

			uploadImage() {
				const uploadTask = uni.uploadFile({
					url: getApp().globalData.url + 'file/upload',
					filePath: this.imagePath,
					name: 'file',
					formData: {},
					success: (uploadFileRes) => {
						let res = JSON.parse(uploadFileRes.data);
						console.log('图片上传结果1：', res);
						if (res.status == 200) {
							this.imageId = res.data.id
						} else {
							this.toast(res.msg)
						}
					},
					fail: (err) => {
						console.log("上传失败: ", err);
					}


				});
			},

			chooseVideo() {
				console.log('choose video...');
				// uni.showLoading({})
				uni.chooseVideo({
					count: 1,
					compressed: false,
					maxDuration: 40,
					sourceType: ['camera', 'album'],
					success: (res) => {
						console.log("视频选择: ", res);
						// uni.hideLoading()
						if(res.duration > 40){
							this.toast('视频时间超过40秒,无法上传');
						}else if (res.size > 62914560) {
							this.toast('视频体积超过60M,无法上传');
						} else {
							this.videoPath = res.tempFilePath
							this.uploadVideo()
						}
					},
					fail: (err) => {
						console.log("选择失败: ", err);
						this.toast('选择文件失败,无法上传')
					}
				})
			},

			uploadVideo() {
				const uploadTask = uni.uploadFile({
					url: getApp().globalData.url + 'file/upload',
					filePath: this.videoPath,
					name: 'file',
					// #ifdef MP-ALIPAY
					fileType: 'video',
					// #endif
					formData: {},
					success: (uploadFileRes) => {
						let res = JSON.parse(uploadFileRes.data);
						console.log('视频上传结果：', res);
						if (res.status == 200) {
							this.videoId = res.data.id
						} else {
							this.toast(res.msg)
						}
					}
				});
			},

			chooseLocation() {
				uni.chooseLocation({
					success: (res) => {
						console.log("选择位置: ", res);
						this.latitude = res.latitude
						this.longitude = res.longitude
						this.address = res.address
					}
				})
			},

			next() {
				//测试
				// this.nextPage()
				// return ;

				//测试
				// if(this.isIOS&&this.publishState==1){
				// 	common.toast('发布成功，已提交审核')
				// 	uni.switchTab({
				// 		url:'/pages/main/index'
				// 	})
				// }
				// return

				if (this.checkDatas()) {
					if (this.publishState == 1) {
						this.toast('发布成功，已提交审核')
						uni.switchTab({
							url: '/pages/main/index'
						})
					} else {
						this.saveOrUpdateAd()
					}

				}


			},
			checkDatas() {
				if (this.publishState == 1) {
					if (!this.title) {
						this.toast('请输入店铺主题')
						return false
					}
					if (!this.content) {
						this.toast('请输入商品介绍')
						return false
					}
					//广告类型 0：图片； 1：视频
					if (this.typeIndex == 0 && !this.imagePath) {
						this.toast('请添加图片')
						return false
					}
					if (this.typeIndex == 1 && !this.imagePath) {
						this.toast('请添加视频封面图片')
						return false
					}
					if (this.typeIndex == 1 && !this.videoPath) {
						this.toast('请添加视频')
						return false
					}
					return true
				} else {
					if (!this.title) {
						this.toast('请输入广告标题')
						return false
					}
					if (!this.content) {
						this.toast('请输入广告内容')
						return false
					}
					if (!this.price) {
						this.toast('请输入单条广告定价')
						return false
					}
					//广告类型 0：图片； 1：视频
					if (this.typeIndex == 0 && !this.imagePath) {
						this.toast('请上传广告图片')
						return false
					}
					if (this.typeIndex == 1 && !this.imagePath) {
						this.toast('请上传视频封面图片')
						return false
					}
					if (this.typeIndex == 1 && !this.videoPath) {
						this.toast('请上传视频')
						return false
					}

					//测试
					// return true

					if (!this.latitude) {
						this.toast('请选择广告投放详细位置')
						return false
					}
					if (!this.distance) {
						this.toast('请输入投放范围')
						return false
					}
					return true
				}

			},

			nextPage() {
				if (this.formIndex == 2) {
					uni.navigateTo({
						url: '/pages/publish/publish-sell?id=' + this.adId + '&amount=' + this.price + 
						'&isUsed=' + this.isUsed + '&channelId=' + this.channelId+ '&pickupMethod=' + this.pickupMethod,
					});
				} else {
					uni.navigateTo({
						url: '/pages/publish/publish-payment?id=' + this.adId + '&amount=' + this.price + 
						'&isUsed=' + this.isUsed + '&channelId=' + this.channelId,
					});
				}
			},
			//带货
			sales(){
				uni.navigateTo({
					url:'./sales'
				})
			},

			saveOrUpdateAd() {
				let appletAppid = ''

				let data = {
					// amount: this.price, //	单条价格		false	integer
					amount: this.price * 100, //	单条价格		false	integer
					belongId: this.mainid, //	所属人		false	string
					// belongId: '1423200188063899649', //	所属人		false	string
					collect: this.isCollect, //	是否收集		false	integer
					content: this.content, //	广告内容		false	string   //createdTime: this.adpri, //	创建时间		false	string
					id: '', //	id		false	string
					picId: this.imageId, //	广告图片		false	string
					sell: this.isSell, //	是否销售		false	integer    //status: this.adpri, //	状态		false	integer
					title: this.title, //	标题		false	string
					type: this.typeIndex, //	类型		false	integer
					videoId: this.videoId, //	视频内容
					placement: this.channelId, //投放渠道 1：支付后 2:视频号
					newUser :this.newSeeId,
					appid: appletAppid,
					typeId: this.categoryId, //视频类目
					pickupMethod: this.pickupMethod, //交易形式
					prodId:this.prod.prodId, //带货商品id
					prodName:this.prod.prodName, //带货商品名称
					pic:this.prod.pic, //带货商品图片
					price:this.prod.price, //带货商品价格
					isUsed:parseInt(this.isUsed), //是否使用卡券
					couponId:this.couponData? this.couponData.id:''
				}
				console.log("saveOrUpdateAd 请求参数: ", data);
				let url = 'ad/saveOrUpdateVideo'
				if(this.channelId === 1){
					url = 'ad/saveOrUpdateAd'
				}
				this.$request.post(url, data, 'json')
					.then(res => {
						console.log("saveOrUpdateAd res:", res)
						this.adId = res.data.data.id
						this.saveOrUpdateLatLon()
					}).catch(err => {
						console.log(" err:", err)
					})
				// common.request(url, data, res => {
				// 	console.log('saveOrUpdateAd：', res)
				// 	this.adId = res.data.id
				// 	//更新广告投放地址？
				// 	this.saveOrUpdateLatLon()
				// })
			},
			// 更新广告投放地址
			saveOrUpdateLatLon() {
				let data = {
					address: this.address, ////	详细地址		false	string
					id: '', //	id		false	string
					name: this.addressName, //	小区名 xx大厦xx层		false	string

					lat: this.latitude,
					lon: this.longitude,
					provinceId: this.provinceId, //	省		false	string
					cityId: this.cityId, //	市		false	string
					areaId: this.areaId, //	区		false	string
					streetId: this.streetId,

					// 测试
					// lat: '36.461843', //	纬度		false	string
					// lon: '116.043516', //	经度		false	string
					// provinceId: '370000', //	省		false	string
					// cityId: '371500', //	市		false	string
					// areaId: '371502', //	区		false	string
					// streetId: "371502009",//街道

				}

				this.$request.post(`area/saveOrUpdateLatLon`, data, 'json')
					.then(res => {
						console.log('saveOrUpdateLatLon：', res)
						this.adressId = res.data.data
						//修改广告配置?
						this.saveOrUpdateAdConfig()
					}).catch(err => {
						console.log(" err:", err)
					})

				// common.request("area/saveOrUpdateLatLon", data, res => {
				// 	console.log('saveOrUpdateLatLon：', res)
				// 	this.adressId = res.data
				// 	//修改广告配置?
				// 	this.saveOrUpdateAdConfig()
				// })
			},
			// 修改广告配置
			saveOrUpdateAdConfig() {
				let data = {
					addressId: this.adressId, //'1384413963656228865',//	地址Id		false							string
					distance: this.distance, //'1384413963656228865',//	广告投放范围		false							string
					id: this.adId, //'1382965821622464513',//	广告id		false							string
				}

				this.$request.post(`ad/saveOrUpdateAdConfig`, data, 'json')
					.then(res => {
						console.log('saveOrUpdateAdConfig：', res)
						if (res.data.status == 200) {
							this.nextPage()
						}
					}).catch(err => {
						console.log(" err:", err)
					})

				// common.request("ad/saveOrUpdateAdConfig", data, res => {
				// 	console.log('saveOrUpdateAdConfig：', res)
				// 	if (res.status == 200) {
				// 		this.nextPage()
				// 	}

				// })
			},
			//获取视频类型
			getCategory() {
				this.$request.post(`ad/getVideoList`, {})
					.then(res => {
						for (let item of res.data.data) {
							this.categoryData.push(item.name)
						}
					}).catch(err => {
						console.log(" err:", err)
					})

				// common.request("video/getVideoList", {}, res => {
				// 	console.log('视频类目：', res)

				// }, err => {
				// 	console.log("视频类目 err: ", err);
				// })
			},

			getProvinces() {
				this.$request.post(`area/areaList/`, {})
					.then(res => {
						console.log('省：', res)
						this.provinceId = res.data.data[0].id
						this.dnui1 = res.data.data;
						for (let item of res.data.data) {
							this.dmultiArray[0].push(item.name)
						}
						// this.dmultiArray[0].splice(0, 1)
						this.getCities()
					}).catch(err => {
						console.log(" err:", err)
					})
				// common.request("area/areaList/", {}, res => {
				// 	// console.log('省：', res)
				// 	this.provinceId = res.data[0].id
				// 	this.dnui1 = res.data;
				// 	for (let item of res.data) {
				// 		this.dmultiArray[0].push(item.name)
				// 	}
				// 	// this.dmultiArray[0].splice(0, 1)
				// 	this.getCities()
				// })
			},
			getCities() {
				console.log("provinceID: " + this.provinceId);
				this.$request.post("area/areaList/" + this.provinceId, {}, )
					.then(res => {
						this.cityId = res.data.data[0].id
						this.dnui2 = res.data.data
						let list = []
						for (let item of res.data.data) {
							list.push(item.name)
						}
						this.dmultiArray.splice(1, 1, list)
						if (!this.cityId) {
							this.cityId = res.data.data[0].id;
						} else {
							for (let i = 0; i < this.dnui2.length; i++) {
								if (this.dpickerid2 == this.dnui2[i].id) {
									this.dmultiIndex[1] = i;
								}
							}
						}
						this.getAreas()
					}).catch(err => {
						console.log(" err:", err)
					})

				// common.request("area/areaList/" + this.provinceId, {}, res => {
				// 	// console.log('市：', res)
				// 	this.cityId = res.data[0].id
				// 	this.dnui2 = res.data
				// 	let list = []
				// 	for (let item of res.data) {
				// 		list.push(item.name)
				// 	}
				// 	this.dmultiArray.splice(1, 1, list)
				// 	if (!this.cityId) {
				// 		this.cityId = res.data[0].id;
				// 	} else {
				// 		for (let i = 0; i < this.dnui2.length; i++) {
				// 			if (this.dpickerid2 == this.dnui2[i].id) {
				// 				this.dmultiIndex[1] = i;
				// 			}
				// 		}
				// 	}
				// 	this.getAreas()
				// })
			},
			getAreas() {
				this.$request.post("area/areaList/" + this.cityId, {})
					.then(res => {
						this.areaId = res.data.data[0].id
						this.dnui3 = res.data.data
						let list = []
						for (let item of res.data.data) {
							list.push(item.name)
						}
						this.dmultiArray.splice(2, 1, list)
						if (!this.areaId) {
							this.areaId = res.data.data[0].id;
						} else {
							for (let i = 0; i < this.dnui3.length; i++) {
								if (this.areaId == this.dnui3[i].id) {
									this.dmultiIndex[2] = i;
								}
							}
						}
						this.getStreets()
					}).catch(err => {
						console.log(" err:", err)
					})
				// common.request("area/areaList/" + this.cityId, {}, res => {
				// 	// console.log('区：', res)
				// 	this.areaId = res.data[0].id
				// 	this.dnui3 = res.data
				// 	let list = []
				// 	for (let item of res.data) {
				// 		list.push(item.name)
				// 	}
				// 	this.dmultiArray.splice(2, 1, list)
				// 	if (!this.areaId) {
				// 		this.areaId = res.data[0].id;
				// 	} else {
				// 		for (let i = 0; i < this.dnui3.length; i++) {
				// 			if (this.areaId == this.dnui3[i].id) {
				// 				this.dmultiIndex[2] = i;
				// 			}
				// 		}
				// 	}
				// 	this.getStreets()
				// })
			},
			getStreets() {

				this.$request.post("area/areaList/" + this.areaId, {}, )
					.then(res => {
						this.streetId = res.data.data[0].id
						this.dnui4 = res.data.data
						let list = []
						for (let item of res.data.data) {
							list.push(item.name)
						}
						this.dmultiArray.splice(3, 1, list)
						if (!this.streetId) {
							this.streetId = res.data.data[0].id;
						} else {
							for (var i = 0; i < this.dnui4.length; i++) {
								if (this.streetId == this.dnui4[i].id) {
									this.dmultiIndex[3] = i;
								}
							}
						}
					}).catch(err => {
						console.log(" err:", err)
					})

				// common.request("area/areaList/" + this.areaId, {}, res => {
				// 	// console.log('街道办：', res)
				// 	this.streetId = res.data[0].id
				// 	this.dnui4 = res.data
				// 	let list = []
				// 	for (let item of res.data) {
				// 		list.push(item.name)
				// 	}
				// 	this.dmultiArray.splice(3, 1, list)
				// 	if (!this.streetId) {
				// 		this.streetId = res.data[0].id;
				// 	} else {
				// 		for (var i = 0; i < this.dnui4.length; i++) {
				// 			if (this.streetId == this.dnui4[i].id) {
				// 				this.dmultiIndex[3] = i;
				// 			}
				// 		}
				// 	}
				// })
			},

			toast(msg) {
				uni.showToast({
					title: msg,
					icon: 'none',
					duration: 2000
				});
			},
			selectProd(prod){
				console.log('选择了商品：',prod)
				this.prod = prod
			}


		}
	}
</script>

<style>
	.publish-page {
		background-color: #F1F1F1;
		padding-bottom: 30upx;
	}

	.publish-page view {
		display: flex;

	}


	.publish-page .item {
		background-color: white;
		width: 690upx;
		margin: 30upx;
		border-radius: 8upx;
		justify-content: space-between;
		align-items: center;
		padding: 20upx 20upx;

	}

	.publish-page .item-padding {

		padding: 30upx 20upx;

	}

	.publish-page .title {
		font-size: 28upx;
	}

	.publish-page .arrow {
		width: 18upx;
		height: 30upx;
	}

	.publish-page .picker {
		flex: 1;
	}

	.publish-page .pciker-content {
		flex: 1;
		flex-direction: row-reverse;
		align-items: center;
	}

	.publish-page .pciker-text {
		margin: 0 20upx;
		font-size: 28upx;
	}

	.publish-page .pciker-text-gray {
		color: #C0C0C0;
	}

	.ad-info {
		font-size: 30upx;
		font-weight: 600;
		margin: 0 30upx;
	}

	.publish-page .item-input {
		text-align: right;
		font-size: 28upx;
	}

	.publish-page .add-wrap {
		width: 100%;
		justify-content: center;
		padding: 30upx 0;
	}

	.publish-page .add-img {
		width: 158upx;
		height: 158upx;
	}

	.publish-page .icon-cloud {
		width: 48upx;
		height: 48upx;
	}

	.publish-page .video-title {
		width: 100%;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.publish-page .video-hint {
		font-size: 28upx;
		color: #F1C75E;
	}

	.publish-page .video-wrap {
		padding: 20upx 0;
	}

	.publish-page .next {
		width: 80%;
		height: 80upx;
		line-height: 80upx;
		color: white;
		background: -webkit-linear-gradient(#F1C75E, #d4a93c);
		background: -o-linear-gradient(#F1C75E, #d4a93c);
		background: -moz-linear-gradient(#F1C75E, #d4a93c);
		background: linear-gradient(#F1C75E, #d4a93c);
		font-size: 32upx;
		border-radius: 10upx;
		margin: 0 auto;
		justify-content: center;

	}

	.publish-page .btn-wrap {
		width: 500upx;
		height: 400upx;
		background-color: white;
		border-radius: 16upx;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.publish-page .btn-wrap .btn-title {
		font-size: 34upx;
		font-weight: 600;
	}

	.publish-page .btn-wrap .btn-hint {
		font-size: 30upx;
		padding: 0 60upx;
		margin-top: 40upx;
	}

	.publish-page .btn-wrap button {
		width: 400upx;
		height: 60upx;
		line-height: 60upx;
		margin-top: 30upx;
		font-size: 28upx;
		border-radius: 30upx;
	}

	.flex {
		display: flex;
		align-items: center;
	}
	.item-prod{
		display: flex;
		justify-content: flex-start !important;
		padding: 20upx;
	}
	.item-prod .prod-img{
		width: 100upx;
		height: 100upx;
		border-radius: 8upx;
	}
	.item-prod .prod-r{
		flex-direction: column;
		margin-left: 30upx;
	}
	.item-prod .prod-text{
		font-size: 28upx;
	}
	.item-prod .prod-price{
		color: firebrick;
		margin-top: 20upx;
	}
	.prod{
		height: 40upx;
		line-height: 40upx;
		font-size: 34upx;
		margin-left: 30upx;
	}

</style>
